<!DOCTYPE html>
<html lang="en">
<head>
    {% load static %}
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static './layui/css/layui.css' %}"    media="all">
    <style>
        body{
            padding: 50px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="form.html">
    <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">学生姓名：</label>
      <div class="layui-input-inline">
        <input type="text" name="user_name" lay-verify="required" autocomplete="off" class="layui-input">
      </div>
    </div>
    </div>
    <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">密码：</label>
      <div class="layui-input-inline">
        <input type="password" name="password" lay-verify="required" autocomplete="off" class="layui-input">
      </div>
    </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">头像：</label>
            <div class="layui-input-inline">

                <div class="layui-upload">
  <button type="button" class="layui-btn" id="test1">上传图片</button>
  <div class="layui-upload-list">
    <img class="layui-upload-img" id="demo1" width="100px" height="100px">
    <p id="demoText"></p>
  </div>
  <div style="width: 95px;">
    <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
      <div class="layui-progress-bar" lay-percent=""></div>
    </div>
  </div>
</div>
                <a name="list-progress"> </a>
                <div style="margin-top: 10px;"></div>
                <input type="hidden" name="img" id="img" value="">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label">性别：</label>
    <div class="layui-input-block">
      <input type="radio" name="sex" value="男" title="男" checked="">
      <input type="radio" name="sex" value="女" title="女">
    </div>
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">籍贯：</label>
    <div class="layui-input-block">
      <select name="address">
        <option value=""></option>
        <option value="广东省">广东省</option>
        <option value="湖南省">湖南省</option>
        <option value="广西壮族自治区">广西壮族自治区</option>
        <option value="江西省">江西省</option>
        <option value="福建省">福建省</option>
      </select>
    </div>
    </div>
    <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">签名：</label>
    <div class="layui-input-block">
      <textarea name="sign" placeholder="请输入您的签名" class="layui-textarea"></textarea>
    </div>
    </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
    </div>
</form>
</body>
<script src="{% static './layui/layui.js'%}"></script>
<script src="{% static './jquery/jquery-3.7.1.min.js'%}"></script>
<script>
    layui.use(['form','upload','element'],function () {
        var form = layui.form
        ,upload = layui.upload
        ,element = layui.element;
        //监听提交
        form.on('submit(demo1)', function(data){
            {#在这里可以写ajax异步请求#}
            $.ajax({
            url:"{% url 'index:student_add_api' %}",
            data:{
                'user_name':data.field.user_name,
                'address':data.field.address,
                'sex':data.field.sex,
                'sign':data.field.sign,
                'img':data.field.img,
                'password':data.field.password,
            },
            type:'POST',
            success:function(result){
                alert(result.msg)
                if(result.code == 0){
                    // 成功
                    // 关闭弹窗，然后刷新table
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    // 然后刷新table
                    // 这里有问题
                    parent.layui.table.reload('test');//重载父页表格，参数为表格ID
                }else{
                    // 失败
                    // 执行页面刷新
                    parent.layui.table.reload('test');
                }
            }
        });
            return false;
        });

        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1' // 绑定
            ,url: '{% url "index:img_upload" %}' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            ,before: function(obj){
              //预读本地文件示例，不支持ie8
              obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
              });

              element.progress('demo', '0%'); //进度条复位
              layer.msg('上传中', {icon: 16, time: 0});
            }
            ,done: function(res){
              //如果上传失败
              if(res.code > 0){
                return layer.msg('上传失败');
              }
              //上传成功的一些操作
              $('#img').attr('value',res.file)
              //……
              $('#demoText').html(''); //置空上传失败的状态
            }
            ,error: function(){
              //演示失败状态，并实现重传
              var demoText = $('#demoText');
              demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
              demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
              });
            }
            //进度条
            ,progress: function(n, elem, e){
              element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
              if(n == 100){
                layer.msg('上传完毕', {icon: 1});
              }
            }
          });
        })
</script>
</html>